package com.frejoys.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.ad.AdPageDto;
import com.frejoys.common.dto.admin.AdAddDto;
import com.frejoys.common.dto.admin.AdUpDto;
import com.frejoys.common.dto.admin.DelIdDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.CommonUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.ad.AdPageVo;
import com.frejoys.service.AdService;
import com.frejoys.service.ResourceLibraryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "广告", description = "广告")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminAdController {

    private final AdService adService;

    private final RedissonClient redissonClient;

    private final ResourceLibraryService resourceLibraryService;

    /**
     * 新增广告
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增广告")
    @PostMapping("/ad/add")
    public Result addInfo(@RequestBody @Validated AdAddDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.addAdLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                adService.addInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    /**
     * 修改广告
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改广告")
    @PostMapping("/ad/up")
    public Result upInfo(@RequestBody @Validated AdUpDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.upAdLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                //获取原广告图
                String cover = adService.getCover(dto.getId());

                //更新
                adService.upInfo(dto);

                resourceLibraryService.delInfo(CommonUtil.getNeedDelImage(cover, dto.getCover()));
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }

    /**
     * 删除广告
     *
     * @param
     * @return
     */
    @Operation(summary = "删除广告")
    @PostMapping("/ad/del")
    public Result delInfo(@RequestBody @Validated DelIdDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.upAdLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                adService.delInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }

    /**
     * 后台查询广告
     */
    @Operation(summary = "后台查询广告")
    @PostMapping("/ad/page")
    public Result<IPage<AdPageVo>> pageListAd(@RequestBody @Validated AdPageDto dto) {
        return Result.success(adService.pageListAd(dto));
    }

}
